package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbProdImageMapper;
import com.bwie.pojo.TbProd;
import com.bwie.pojo.TbProdImage;
import com.bwie.service.TbProdService;
import com.bwie.mapper.TbProdMapper;
import com.bwie.utils.Result;
import com.bwie.vo.AttaVo;
import com.bwie.vo.ProdPageVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【tb_prod(商品表)】的数据库操作Service实现
* @createDate 2025-05-27 09:23:59
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class TbProdServiceImpl extends ServiceImpl<TbProdMapper, TbProd>
    implements TbProdService{

    @Autowired
    TbProdMapper tbProdMapper;
    @Autowired
    HttpServletRequest request;
    @Autowired
    FastFileStorageClient fastFileStorageClient;
    @Autowired
    TbProdImageMapper tbProdImageMapper;

    @Override
    public Result getProdList(ProdPageVo pageVo) {
        Page<TbProd> page = new Page<>(pageVo.getPageNum(), pageVo.getPageSize());

        LambdaQueryWrapper<TbProd> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(pageVo.getKeyWord())){
            wrapper.like(TbProd::getProdName,pageVo.getKeyWord());
        }


        Page<TbProd> tbProdPage = tbProdMapper.selectProdPage(page, pageVo);
        return Result.ok().setData(tbProdPage);
    }

    @Override
    public Result loadImage(MultipartFile file) {
        try {
            String extName = FileNameUtil.extName(file.getOriginalFilename());

            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            String url = "http://192.168.80.192:8888/"+storePath.getFullPath();

            return Result.ok().setData(url);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error().setMsg("上传失败");
        }
    }

    @Override
    public Result addProd(TbProd tbProd) {
        TbProd prod = tbProdMapper.selectOne(
                new LambdaQueryWrapper<TbProd>().eq(TbProd::getProdSpu, tbProd.getProdSpu())
        );
        if(prod != null){
            return Result.error().setMsg("该商品SPU已经存在");
        }

        String token = request.getHeader("token");
        if(token == null){
            return Result.error().setMsg("请先登录");
        }
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String nickName = body.get("nickName", String.class);

        tbProd.setCreateBy(nickName);
        tbProd.setCreateTime(new Date());
        tbProdMapper.insert(tbProd);
        return Result.ok();
    }

    @Override
    public Result deleteBatch(List<TbProd> ids) {
        if(ids == null || ids.size()<=0){
            return Result.error().setMsg("请先选择要删除的数据");
        }
        for (TbProd id : ids) {
            tbProdMapper.deleteById(id);
        }
        return Result.ok();
    }

    @Override
    public Result addProdAtta(AttaVo attaVo) {
        //确定商品是否存在
        TbProd tbProd = tbProdMapper.selectById(attaVo.getProdId());
        if(tbProd == null){
            return Result.error().setMsg("商品不存在");
        }
        //更新商品信息
        tbProd.setProdVideo(attaVo.getProdVideo());
        tbProd.setUpType(attaVo.getUpType());
        tbProd.setUpTime(attaVo.getUpTime());
        tbProd.setDownType(attaVo.getDownType());
        tbProd.setDownTime(attaVo.getDownTime());
        tbProdMapper.updateById(tbProd);

        tbProdImageMapper.delete(
                new LambdaQueryWrapper<TbProdImage>().eq(TbProdImage::getProdId,tbProd.getProdId())
        );

        //添加商品轮播图
        if(attaVo.getImageList() != null && attaVo.getImageList().size() > 0){
            for (String imageUrl : attaVo.getImageList()) {
                TbProdImage tbProdImage = new TbProdImage();
                tbProdImage.setProdId(tbProd.getProdId());
                tbProdImage.setImageUrl(imageUrl);

                tbProdImageMapper.insert(tbProdImage);
            }
        }

        return Result.ok();
    }
}




